﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using LPV_POS.mh_pos.mh_ban_hang;
//using LPV_POS.In;

namespace LPV_POS.mh_pos
{
    public partial class mh_banhang8_Tinh_Tien_HOTELDEM : DevExpress.XtraEditors.XtraForm
    {
        DataTable tbHT_NGAY_LE = null;
        #region "   Khai bao"
        DataTable tbBH_PHIEU_TMP = null;
        DataTable tbBH_PHIEU_CT_TMP = null;
        //DataTable tbDonGia = null;
        int iGIO_VAO_QUY_DINH = 0;
        //int iGIO_VAO_QUY_DINH_T7CNLE = 0;
        int iGIO_RA_QUY_DINH = 0;
        //int iGIO_RA_QUY_DINH_T7CNLE = 0;
        #endregion

        #region "   mh_banhang8_Tinh_Tien_HOTELDEM"
        public mh_banhang8_Tinh_Tien_HOTELDEM(DataTable _tbBH_PHIEU_TMP, DataTable _tbBH_PHIEU_CT_TMP)
        {
            InitializeComponent();
            tbBH_PHIEU_TMP = _tbBH_PHIEU_TMP;
            tbBH_PHIEU_CT_TMP = _tbBH_PHIEU_CT_TMP;

            this.SetgrdDonGia_DataSource();
            txtGhiChu.Text = _tbBH_PHIEU_TMP.Rows[0]["GHI_CHU"] + "";

        }
        #endregion

        #region "   SetgrdDonGia_DataSource"
        void SetgrdDonGia_DataSource()
        {
            try
            {
                DataTable tbDG = tbBH_PHIEU_CT_TMP.Copy();
                for (int i = 0; i < tbDG.Rows.Count; i++)
                {
                    if (string.Concat(tbDG.Rows[i]["MAHH"], "").IndexOf("Tien_Gio") < 0)
                    {
                        tbDG.Rows.RemoveAt(i);
                        i--;
                    }
                }
                for (int i = 0; i < tbDG.Rows.Count; i++)
                    tbDG.Rows[i]["STT"] = i + 1;
                grdTienPhong.DataSource = tbDG;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion
        Boolean iLoadForm = false;
        #region "   mh_banhang8_Tinh_Tien_HOTELDEM_Load"
        private void mh_banhang8_Tinh_Tien_HOTELDEM_Load(object sender, EventArgs e)
        {
            this.ShowInfo();

            this.Load_DonGia();
            iLoadForm = true;
            this.dtpVao_ValueChanged(null, null);
        }
        #endregion

        #region "   ShowForm()"
        float fso = 0;
        public float ShowForm()
        {
            this.ShowDialog();
            return fso;
        }
        #endregion

        #region "   ShowInfo()"
        void ShowInfo()
        {
            try
            {

                string ichkKaraokeAuTo = new cpublic().Registry_Get("chkKaraokeAuTo");
                chkKaraokeAuTo.Checked = ichkKaraokeAuTo == "True";
                btnTinhTienGio.Enabled = !chkKaraokeAuTo.Checked;

                DateTime dNGAY_GIO_VAO;
                if (tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] + "" == "")
                    tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] = Variable.ngay_gio_hienhanh;

                DateTime.TryParse(tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] + "", out dNGAY_GIO_VAO);
                dtpVao.Value = dNGAY_GIO_VAO;

                //
                dtpRa.Value = Variable.ngay_gio_hienhanh;
                //
                decimal dGiam_PhanTram = 0;
                if (tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length > 0)
                    decimal.TryParse(tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[0]["CHIETKHAU_PHANTRAM"] + "", out dGiam_PhanTram);
                else
                {
                    #region "   Giam Gia Default"
                    cpublic ocpublic = new cpublic();
                    string d1 = ocpublic.Registry_Get("dtpGiamTienGio_Tu");
                    string d2 = ocpublic.Registry_Get("dtpGiamTienGio_Den");
                    if (d1.Length == 8 && d2.Length == 8)
                    {
                        string d3 = ocpublic.Registry_Get("nudGiam_TienGio_PhanTram");
                        //tu ngay
                        string l1 = d1.Substring(0, 4);
                        string l2 = d1.Substring(4, 2);
                        string l3 = d1.Substring(6, 2);
                        DateTime dngay1 = new DateTime(int.Parse(l1), int.Parse(l2), int.Parse(l3), 0, 0, 0);

                        //den ngay
                        l1 = d2.Substring(0, 4);
                        l2 = d2.Substring(4, 2);
                        l3 = d2.Substring(6, 2);
                        DateTime dngay2 = new DateTime(int.Parse(l1), int.Parse(l2), int.Parse(l3), 23, 59, 59);

                        if (dngay1 <= dtpVao.Value && dtpVao.Value <= dngay2)
                        {
                            decimal dc = 0;
                            decimal.TryParse(d3, out dc);
                            dGiam_PhanTram = dc;
                        }
                    }
                    #endregion
                }
                nudGiam_PhanTram.Value = dGiam_PhanTram;
            }
            catch (Exception ex)
            {
                string sx = ex.Message;
            }
        }
        #endregion

        #region "   btnghichu_Click"
        private void btnghichu_Click(object sender, EventArgs e)
        {
            mh_chu omh_chu = new mh_chu();
            string str = omh_chu.ShowForm("Ghi chú", txtGhiChu.Text, 300, 300, false);
            if (omh_chu.DialogResult == DialogResult.OK)
                txtGhiChu.Text = str;
        }
        #endregion

        private void btnTinhTienGio_Click(object sender, EventArgs e)
        {
            #region "   sT"
            string sT = "";
            if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Monday)
                sT = "T2";
            else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Tuesday)
                sT = "T3";
            else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Wednesday)
                sT = "T4";
            else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Thursday)
                sT = "T5";
            else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Friday)
                sT = "T6";
            else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Saturday)
                sT = "T7";
            else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Sunday)
                sT = "T1";
            lblNgay_Gio_Vao.Text = lblNgay_Gio_Vao.Tag + " (" + sT.Replace("T1", "CN") + ")";
            #endregion

            thuvien othuvien = new thuvien();
            DataTable tbTinhTienGio = othuvien.SP_SelectDataTable("sp_TinhTienGio",
                                                            new string[] { "@flag", "@gio_vao", "@gio_ra", "@tienDM", "@DM_TIENGIO5_OBJID" ,
                                                                            "@GIO_VAO_QUY_DINH","@GIO_RA_QUY_DINH"//,"@GIO_VAO_QUY_DINH_T7CNLE","@GIO_RA_QUY_DINH_T7CNLE"
                                                            },
                                                            new object[] { 5, dtpVao.Value, dtpRa.Value, nudDonGia.Value, nudDonGia.Tag,
                                                                            iGIO_VAO_QUY_DINH, iGIO_RA_QUY_DINH//, iGIO_VAO_QUY_DINH_T7CNLE, iGIO_RA_QUY_DINH_T7CNLE
                                                            });
            if (tbTinhTienGio.Rows.Count == 0)
            {
                DataRow drt = tbTinhTienGio.NewRow();
                drt["SO_LUONG"] = 1;
                drt["DON_GIA"] = nudDonGia.Value;
                tbTinhTienGio.Rows.Add(drt);
            }

            if (tbTinhTienGio.Rows.Count > 0)
            {
                DataTable tb = (DataTable)grdTienPhong.DataSource;
                for (int i = 0; i < tbTinhTienGio.Rows.Count; i++)
                {
                    if (tb.Rows.Count < i + 1)
                    {
                        DataRow dr = tb.NewRow();
                        tb.Rows.Add(dr);
                    }

                    tb.Rows[i]["SO_LUONG"] = tbTinhTienGio.Rows[i]["SO_LUONG"];
                    tb.Rows[i]["DON_GIA"] = tbTinhTienGio.Rows[i]["DON_GIA"];
                    if (tbTinhTienGio.Rows[i]["ca_ngay"] + "" == "True")
                    {
                        tb.Rows[i]["GHI_CHU"] = "";
                        tb.Rows[i]["MAHH"] = "Tien_Gio";
                        tb.Rows[i]["OBJNAME_HH_ROOT"] = "Tiền phòng";
                        tb.Rows[i]["OBJNAME_HH"] = "Tiền phòng";
                    }
                    else
                    {
                        tb.Rows[i]["GHI_CHU"] = tbTinhTienGio.Rows[i]["GHI_CHU"];
                        tb.Rows[i]["MAHH"] = "Tien_Gio_Them";
                        tb.Rows[i]["OBJNAME_HH_ROOT"] = "Tiền phòng thêm";
                        tb.Rows[i]["OBJNAME_HH"] = "Tiền phòng thêm";
                    }
                }

                for (int i = tbTinhTienGio.Rows.Count; i < tb.Rows.Count; i++)
                    tb.Rows.RemoveAt(i--);
            }
            grdTienPhong.Refresh();
        }

        #region "   btnThoat_Click"
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }
        #endregion

        #region "   btnThucHien_Click"
        private void btnThucHien_Click(object sender, EventArgs e)
        {//Luu Update
            try
            {
                //BH_PHIEU_TMP
                tbBH_PHIEU_TMP.Rows[0]["GHI_CHU"] = txtGhiChu.Text.Trim();
                tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_RA"] = dtpRa.Value;
                tbBH_PHIEU_TMP.Rows[0]["NGAY_GIO_VAO"] = dtpVao.Value;

                //BH_PHIEU_CT_TMP
                DataTable tbTienGio_ChiTiet = (DataTable)grdTienPhong.DataSource;

                #region "   UPDATE lại dữ liệu TIỀN phòng vào tbBH_PHIEU_CT_TMP"
                if (tbTienGio_ChiTiet != null)
                {
                    float iSL = 0;
                    int iDG = 0;
                    double iGiamGia = 0;
                    for (int i = 0; i < tbTienGio_ChiTiet.Rows.Count; i++)
                    {
                        if (tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length < i + 1)
                        {
                            DataRow dr = null;
                            dr = tbBH_PHIEU_CT_TMP.NewRow();
                            dr["MAHH"] = "Tien_Gio";
                            dr["CHIETKHAU_TIEN"] = 0;
                            dr["KM_TIEN"] = 0;
                            dr["VAT_TIEN"] = 0;
                            dr["GHI_CHU"] = "";
                            tbBH_PHIEU_CT_TMP.Rows.Add(dr);
                        }
                        //
                        float.TryParse(tbTienGio_ChiTiet.Rows[i]["SO_LUONG"] + "", out iSL);
                        int.TryParse(tbTienGio_ChiTiet.Rows[i]["DON_GIA"] + "", out iDG);

                        decimal dPhanTram = nudGiam_PhanTram.Value;

                        object iTT = iSL * iDG;
                        iGiamGia = this.tinh_phantram(iTT + "", dPhanTram.ToString().Replace(",", ""));
                        //
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["MAHH"] = tbTienGio_ChiTiet.Rows[i]["MAHH"];
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["OBJNAME_HH_ROOT"] = tbTienGio_ChiTiet.Rows[i]["OBJNAME_HH_ROOT"];
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["OBJNAME_HH"] = tbTienGio_ChiTiet.Rows[i]["OBJNAME_HH"];
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["SO_LUONG"] = tbTienGio_ChiTiet.Rows[i]["SO_LUONG"];
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["DON_GIA"] = tbTienGio_ChiTiet.Rows[i]["DON_GIA"];
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["CHIETKHAU_PHANTRAM"] = nudGiam_PhanTram.Value;
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["CHIETKHAU_TIEN"] = iGiamGia;
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["NGAY_TINH_GIO"] = dtpRa.Value;
                        tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["GHI_CHU"] = tbTienGio_ChiTiet.Rows[i]["GHI_CHU"];
                    }
                }
                #endregion

                #region "   TAM THOI ko xai"
                //DataRow dr = null;
                //if (tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length > 0)
                //    dr = tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[0];
                //else
                //{
                //    dr = tbBH_PHIEU_CT_TMP.NewRow();
                //    dr["MAHH"] = "Tien_Gio";
                //    tbBH_PHIEU_CT_TMP.Rows.Add(dr);
                //    dr["CHIETKHAU_TIEN"] = 0;
                //    dr["KM_TIEN"] = 0;
                //    dr["VAT_TIEN"] = 0;
                //    dr["GHI_CHU"] = "";
                //}
                //dr["OBJNAME_HH_ROOT"] = "Tiền phòng";
                //dr["OBJNAME_HH"] = "Tiền phòng";
                //dr["SO_LUONG"] = nudTong_Gio.Value;
                //dr["DON_GIA"] = nudDonGia.Value;
                //dr["NGAY_TINH_GIO"] = dtpRa.Value;
                //dr["THANH_TIEN"] = nudThanhTien.Value;
                #endregion

                #region "   Xoa nhung dong ko nam trong TIEN PHONG CHI TIET"
                for (int i = tbTienGio_ChiTiet.Rows.Count; i < tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'").Length; i++)
                {
                    Int64 iobjid = 0;
                    Int64.TryParse(tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]["OBJID"] + "", out iobjid);
                    if (iobjid > 0)
                    {
                        string sSQL = "DELETE FROM BH_PHIEU_CT_TMP WHERE OBJID = " + iobjid;
                        int irt = new thuvien().SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
                    }
                    tbBH_PHIEU_CT_TMP.Rows.Remove(tbBH_PHIEU_CT_TMP.Select("MAHH LIKE 'Tien_Gio%'")[i]);
                    i--;

                }
                #endregion
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                MessageBox_LPV.Show("Thông báo\n" + ex.Message);
            }
        }
        #endregion

        #region "   tinh_phantram"
        Double tinh_phantram(string _sThanhTien, string phantram)
        {//tinh cho ca CHIET_KHAU va VAT

            double _ThanhTien = double.Parse(_sThanhTien.Replace(",", ""));
            Double db = 0;
            try
            {
                db = Double.Parse("0" + phantram.Replace("%", ""));
                db = Math.Round(_ThanhTien * db / 100f / 100, 0) * 100;//lam tron 2 so (123,123.45 --> 123,100)
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return db;
        }
        #endregion

        private void chkKaraokeAuTo_CheckedChanged(object sender, EventArgs e)
        {
            btnTinhTienGio.Enabled = !chkKaraokeAuTo.Checked;
            new cpublic().Registry_Set("chkKaraokeAuTo", chkKaraokeAuTo.Checked);
        }

        private void dtpVao_ValueChanged(object sender, EventArgs e)
        {
            if (iLoadForm)
            {
                this.Load_DonGia();
                if (chkKaraokeAuTo.Checked)
                    this.btnTinhTienGio_Click(null, null);
            }
        }

        void Load_DonGia()
        {
            #region "   Load Don Gia"
            thuvien othuvien = new thuvien();
            DataTable tbDonGia = othuvien.SP_SelectDataTable("sp_DM_TIENGIO5",
                                                         new string[] { "@flag", "@DM_BAN_MA" },
                                                         new object[] { 7, tbBH_PHIEU_TMP.Rows[0]["DM_BAN_MA"] });
            if (tbDonGia.Rows.Count > 0)
            {
                #region "   sT"
                string sT = "";
                if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Monday)
                    sT = "T2";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Tuesday)
                    sT = "T3";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Wednesday)
                    sT = "T4";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Thursday)
                    sT = "T5";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Friday)
                    sT = "T6";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Saturday)
                    sT = "T7";
                else if (dtpVao.Value.DayOfWeek == System.DayOfWeek.Sunday)
                    sT = "T1";
                lblNgay_Gio_Vao.Text = lblNgay_Gio_Vao.Tag + " (" + sT.Replace("T1", "CN") + ")";
                #endregion
                if (tbHT_NGAY_LE == null)
                    tbHT_NGAY_LE = new thuvien().SP_SelectDataTable("sp_HT_NGAY_LE",
                                                   new string[] { "@flag", "@NAM" },
                                                   new object[] { 5, dtpVao.Value.Year });
                if (tbHT_NGAY_LE.Select("ngay1 = '" + dtpVao.Value.ToString("yyyyMMdd") + "'").Length == 0)
                {
                    int.TryParse(tbDonGia.Rows[0]["GIO_VAO_QUY_DINH"] + "", out iGIO_VAO_QUY_DINH);
                    int.TryParse(tbDonGia.Rows[0]["GIO_RA_QUY_DINH"] + "", out iGIO_RA_QUY_DINH);
                }
                else
                {
                    int.TryParse(tbDonGia.Rows[0]["GIO_VAO_QUY_DINH_T7CNLE"] + "", out iGIO_VAO_QUY_DINH);
                    int.TryParse(tbDonGia.Rows[0]["GIO_RA_QUY_DINH_T7CNLE"] + "", out iGIO_RA_QUY_DINH);
                }
                if (iGIO_VAO_QUY_DINH > 12)
                {
                    lblDonGia_1Dem.Text = lblDonGia_1Dem.Tag.ToString().Replace("@in", (iGIO_VAO_QUY_DINH - 12) + "").Replace("@out", iGIO_RA_QUY_DINH + "") + "";
                    groupBox1.Text = groupBox1.Tag.ToString().Replace("@in", (iGIO_VAO_QUY_DINH - 12) + "").Replace("@out", iGIO_RA_QUY_DINH + "") + "";
                }
                else
                {
                    lblDonGia_1Dem.Text = lblDonGia_1Dem.Tag.ToString().Replace("@in", iGIO_VAO_QUY_DINH + "").Replace("@out", iGIO_RA_QUY_DINH + "") + "";
                    groupBox1.Text = groupBox1.Tag.ToString().Replace("@in", iGIO_VAO_QUY_DINH + "").Replace("@out", iGIO_RA_QUY_DINH + "") + "";
                }
                decimal dgia = 0;
                decimal.TryParse(tbDonGia.Rows[0][sT] + "", out dgia);
                if (tbDonGia.Rows[0]["TINH_GIA_USD"] + "" == "True")
                {
                    int iTI_GIA_USD = 0;
                    int.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["TI_GIA_USD"] + "", out iTI_GIA_USD);
                    dgia = dgia * iTI_GIA_USD;
                }
                if (dgia > 1000000000)
                {
                    MessageBox_LPV.Show("cấu hình tiền phòng quá lớn\nVui lòng xem lại phần danh mục tiền giờ");
                    return;
                }
                else
                    nudDonGia.Value = dgia;
                nudDonGia.Tag = tbDonGia.Rows[0]["OBJID"];
                //////////////////////////////////////////
                DataTable tbDM_TIENGIO5_THEM_GIO = othuvien.SP_SelectDataTable("sp_DM_TIENGIO5_THEM_GIO",
                                                                                new string[] { "@flag", "@DM_TIENGIO5_OBJID" },
                                                                                new object[] { 4, tbDonGia.Rows[0]["OBJID"] });
                grdDM_TIENGIO5_THEM_GIO.DataSource = tbDM_TIENGIO5_THEM_GIO;
            }

            #endregion
        }

        private void nudDonGia_ValueChanged(object sender, EventArgs e)
        {
            if (iLoadForm)
                if (chkKaraokeAuTo.Checked)
                    this.btnTinhTienGio_Click(null, null);
        }

        private void btnCauHinh_Click(object sender, EventArgs e)
        {
            mh_banhang8_Giam_TienGio frm = new mh_banhang8_Giam_TienGio();
            frm.ShowDialog();
        }
    }
}